// ActionScript file
import com.adobe.cairngorm.control.CairngormEventDispatcher;

import mx.events.CloseEvent;
import mx.events.ValidationResultEvent;
import mx.managers.PopUpManager;

import net.optix.ip.events.EditPersonalInformationEvent;
import net.optix.ip.model.ModelLocator;
import net.optix.ip.vo.UserVO;

[Bindable]
public var model:ModelLocator	=	ModelLocator.getInstance(); 
  
public function init():void
{
	model.bSubmit	=	true;
	model.result	=	"";
	model.close		=	"no";				
}
  			
private function titleWindow_close(evt:CloseEvent):void { 
	model.bSubmit	=	true;
   	model.result	=	"";
  	PopUpManager.removePopUp(this); 
}
            
public function editPersonalInformation():void
{	
    var result:Boolean	=	true;

	var resultValidate:ValidationResultEvent;
				
	resultFirstName.text	=	"";
	resultLastName.text		=	"";
	resultAddress.text	=	"";
	resultCity.text	=	"";
	resultState.text		=	"";
	resultPostalCode.text	=	"";
	resultCountry.text		=	"";
	resultWorkPhone.text	=	"";
	resultHomePhone.text	=	"";
	resultCellPhone.text	=	"";
	resultFax.text			=	"";
	model.result			=	"";
				
	resultValidate	=	stringValidator.validate(firstName.text);
    if(resultValidate.type == ValidationResultEvent.INVALID)
    {
     	result	=	false;
    	resultFirstName.text	=	
		resultValidate.message;
	}
            	
	resultValidate	=	stringValidator.validate(lastName.text);
	if(resultValidate.type == ValidationResultEvent.INVALID)
   	{
   		result	=	false;
    	resultLastName.text	=	
       	resultValidate.message;
  	}

    if(postalCode.text != '')
    {
    	resultValidate	=	postalValidator.validate(postalCode.text);
	  	if(resultValidate.type == ValidationResultEvent.INVALID)
	   	{
	      	result	=	false;
	     	resultPostalCode.text	=	
	     	resultValidate.message;
	   	}
    }      	
 	
    
    if(workPhone.text != "")
    {  	
	  	resultValidate	=	phoneValidator.validate(workPhone.text);
	 	if(resultValidate.type == ValidationResultEvent.INVALID)
	 	{
	 		result	=	false;
	    	resultWorkPhone.text	=	
	    	resultValidate.message;
	 	}
    }
    
    if(homePhone.text != "")
    {       	
	 	resultValidate	=	phoneValidator.validate(homePhone.text);
	 	if(resultValidate.type == ValidationResultEvent.INVALID)
	  	{
	       	result	=	false;
	       	resultHomePhone.text	=	
	       	resultValidate.message;
	 	}
    }
    
    if(cellPhone.text != "")
    {        	
	  	resultValidate	=	phoneValidator.validate(cellPhone.text);
	 	if(resultValidate.type == ValidationResultEvent.INVALID)
	  	{
	     	result	=	false;
	     	resultCellPhone.text	=	
	       	resultValidate.message;
	 	}
    }
    
    if(fax.text !="")
    {
    	resultValidate	=	phoneValidator.validate(fax.text);
	 	if(resultValidate.type == ValidationResultEvent.INVALID)
	  	{
	      	result	=	false;
	      	resultFax.text	=	
	      	resultValidate.message;
	  	}
    }   	
      	
 	model.bSubmit	=	false;
	model.result	=	"";
       	
  	if(result == true)
  	{
       	var vo:UserVO = model.user;
            	
    	vo.firstName	=	firstName.text;
      	vo.lastName		=	lastName.text;
     	vo.address		=	address.text;
      	vo.city			=	city.text;
       	vo.state		=	state.text;
      	vo.postalCode	=	postalCode.text;
       	vo.country		=	country.text;
     	vo.workPhone	=	workPhone.text;
     	vo.homePhone	=	homePhone.text;
      	vo.cellPhone	=	cellPhone.text;
      	vo.fax			=	fax.text;
            		
     	var event:EditPersonalInformationEvent	=	new EditPersonalInformationEvent(vo);
      	CairngormEventDispatcher.getInstance().dispatchEvent( event );
 	}
  	else
    {
   		model.bSubmit	=	true;
	}
}
            
public function clear():void
{
	
}